package com.zsk.shop.controller.app;

import com.zsk.shop.api.pay.common.bean.RefundOrder;
import com.zsk.shop.common.annotion.NoPermission;
import com.zsk.shop.common.base.BaseResponse;
import com.zsk.shop.common.constant.Constant;
import com.zsk.shop.config.pay.WeixinPayConfig;
import com.zsk.shop.service.BasePayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @author Ys
 * @date 2022年03月01日 20:01
 */
@RestController
@Api(tags = "商城-支付相关")
@RequestMapping("/pay")
public class PayController {

    @Resource
    private BasePayService baseApyService;
    @Resource
    private WeixinPayConfig weixinPayConfig = null;

    @GetMapping(value = "/queryOrderInfo.do")
    @ApiOperation("获取订单状态(支付宝)")
    @ApiImplicitParam(name = "orderNo", value = "订单号")
    public BaseResponse queryOrderInfo(String orderNo) {
        return baseApyService.result(orderNo);
    }


    @GetMapping(value = "/updateOrderStatus.do")
    @ApiOperation("更新订单状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "tradeNo", value = "订单号"),
            @ApiImplicitParam(name = "totalAmount", value = "实付金额")})
    public BaseResponse updateOrderStatus(String tradeNo, BigDecimal totalAmount) {
        if (!Constant.isProd()) {
            baseApyService.paySuccess(tradeNo, totalAmount, null);
        }

        return BaseResponse.success();
    }


    /**
     * 微信支付回调
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @NoPermission
    @RequestMapping(value = "/wxShopNotify.do")
    public void wxNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        baseApyService.wxNotify(request, response);
    }

    /**
     * 支付回调地址
     *
     * @param request
     * @return
     */
    @NoPermission
    @RequestMapping(value = "/aliPayNotify.do")
    public String payBack(HttpServletRequest request) {
        return baseApyService.aliPayNotify(request);
    }

    /**
     * 申请退款接口
     *
     * @return 返回支付方申请退款后的结果
     */
    @NoPermission
    @PostMapping("/refund.do")
    public Map<String, Object> refund(@RequestBody RefundOrder refundOrder) {
        if ("ssl 退款证书".equals("")) {
            throw new RuntimeException("请设置好SSL退款证书");
        }
        refundOrder.setRefundNo("TGSP1555317584802");
        refundOrder.setOutTradeNo("TGSP1555317584802");
        refundOrder.setRefundAmount(new BigDecimal(0.01));
        refundOrder.setTotalAmount(new BigDecimal(0.01));
        return weixinPayConfig.getPayServiceMINI().refund(refundOrder);
    }

    /**
     * 查询退款
     *
     * @param order 订单的请求体
     * @return 返回支付方查询退款后的结果
     */
    @NoPermission
    @RequestMapping("refundQuery.do")
    public Map<String, Object> refundquery(RefundOrder order) {
        return weixinPayConfig.getPayServiceMINI().refundquery(order);
    }
}
